package com.jason.canvaslayer.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * @DESC:
 * @Author: Jason
 * @Date: 16/9/26
 * @Time: 14:55
 */

public class CanvasSaveView3 extends View {

    private Paint mPaint;
    private int mViewWidth, mViewHeight;

    public CanvasSaveView3(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mViewHeight = h;
        mViewWidth = w;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mPaint.setColor(Color.RED);
        canvas.drawRect(mViewWidth / 2F - 200, mViewHeight / 2F - 200,
                mViewWidth / 2F + 200, mViewHeight / 2F + 200, mPaint);

        // 保存并裁剪画布填充绿色
        int saveID1 = canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipRect(mViewWidth / 2F - 200, mViewHeight / 2F - 200,
                mViewWidth / 2F + 200, mViewHeight / 2F + 200);
        canvas.drawColor(Color.GREEN);

        // 保存画布并旋转后绘制一个蓝色的矩形
        int saveID2 = canvas.save(Canvas.MATRIX_SAVE_FLAG);

        // 旋转画布
        canvas.rotate(5);
        mPaint.setColor(Color.BLUE);
        canvas.drawRect(mViewWidth / 2F - 100, mViewHeight / 2F - 100,
                mViewWidth / 2F + 100, mViewHeight / 2F + 100, mPaint);
        canvas.restoreToCount(saveID2);

        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(mViewWidth / 2F - 400, mViewHeight / 2F - 400,
                mViewWidth / 2F + 400, mViewHeight / 2F + 400, mPaint);


    }
}
